<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/database.php";
require_once $basedir."/inc/adress.php";
require_once $basedir."/inc/gruppen.php";

class Mitarbeiter {
    
    public static $TABLENAME = "mitarbeiter";
    public static $CLASSNAME = "Mitarbeiter";
    
    private $id = "";
    private $adress_id = "";
    private $rolle = "Keine";
    private $gruppen_id = 0;
    private $eingetreten_am = "";
    private $ausgeschieden_am = "";
    private $bilder_druck_erlaubt = 0;
    private $bilder_internet_erlaubt = 0;
    
    public function __construct($id="") {
        if($id!='') {
            $temp = Database::getDefault()->openId("mitarbeiter",$id);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
        }
    }
    
    public function getJsonData() {
        $var = get_object_vars($this);
        foreach($var as &$value) {
            if(is_object($value) && method_exists($value, 'getJsonData')) {
                $value = $value->getJsonData();
            }
        }
        return $var;
    }
    
    public function delete() {
        Database::getDefault()->delete("mitarbeiter",$this->id);
    }
    
    public function save() {
        $values = array();
        foreach ($this as $key => $value) {
            $values[$key] = $value;
        }
        if($this->id=="") {
            $this->id = Database::getDefault()->insert("mitarbeiter", $values);
        } else {
            Database::getDefault()->update("mitarbeiter", $values, $this->id);
        }
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function setAdresseId($value) {
        $this->adress_id = $value;
    }
    
    public function getAdresseId() {
        return $this->adress_id;
    }
    
    public function getAdress() {
        return new Adresse($this->getAdresseId());
    }

    public static function getRollen() {
        $array = array('Kene','Gruppenleitung','Erzieher','Vorstand','Praktikant');
        return $array;
    }
    
    public function setRolle($value) {
        $this->rolle = $value;
    }
    
    public function getRolle() {
        return $this->rolle;
    }
    
    public function setGruppenId($value) {
        $this->gruppen_id = $value;
    }
    
    public function getGruppenId() {
        return $this->gruppen_id;
    }
    
    public function getGruppe() {
        return new Gruppen($this->getGruppenId());
    }
    
    public function setEingetrettenAm($value) {
        $this->eingetreten_am = $value;
    }
    
    public function getEingetettenAm() {
        return $this->eingetreten_am;
    }
    
    public function setAusgeschiedenAm($value) {
        $this->ausgeschieden_am = $value;
    }
    
    public function getAusgeschiedenAm() {
        return $this->ausgeschieden_am;
    }
    
    public function setBilderDruckErlaubt($value) {
        $this->bilder_druck_erlaubt = $value;
    }
    
    public function isBilderDruckErlaubt() {
        return $this->bilder_druck_erlaubt;
    }
    
    public function setBilderInternetErlaubt($value) {
        $this->bilder_internet_erlaubt = $value;
    }
    
    public function isBilderInternetErlaubt() {
        return $this->bilder_internet_erlaubt;
    }
    
    public static function getAll($onlyActiv=true, $rolle="", $gruppen_id="") {
        global $VEREIN;
        $query = "SELECT Mitarbeiter.* FROM " . $VEREIN['prefix'] . "mitarbeiter AS Mitarbeiter, " . $VEREIN['prefix'] . "adresse AS Adresse WHERE Adresse.id=Mitarbeiter.adress_id";
        if($onlyActiv) {
            $query .= " AND (ausgeschieden_am>=NOW() OR ausgeschieden_am='0000-00-00')";
        }
        if($rolle!="") {
            $query .= " AND Mitarbeiter.rolle='$rolle'";
        }
        if($gruppen_id!="") {
            $query .= " AND Mitarbeiter.gruppen_id='$gruppen_id'";
        }
        $query .= " ORDER BY Adresse.nachname, Adresse.vorname";
        return Database::getDefault()->execute($query, "Mitarbeiter");
    }
    
    public static function getVorstand($onlyActiv=true) {
        return Mitarbeiter::getAll($onlyActiv, "Vorstand");
    }
    
    public static function getZweitkraft($onlyActiv=true, $gruppen_id="") {
        return Mitarbeiter::getAll($onlyActiv, "Erzieher", $gruppen_id);
    }
    
    public static function getErstkraft($onlyActiv=true, $gruppen_id="") {
        return Mitarbeiter::getAll($onlyActiv, "Gruppenleitung", $gruppen_id);
    }
    
    /**
     * create table for addresses or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>create / update ". $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN adress_id INT UNSIGNED DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN rolle VARCHAR(20) DEFAULT 'Keine'";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN gruppen_id INT UNSIGNED DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN eingetreten_am DATE DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN ausgeschieden_am DATE DEFAULT NULL";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN bilder_druck_erlaubt TINYINT(1) DEFAULT 0";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Mitarbeiter::$TABLENAME . " ADD COLUMN bilder_internet_erlaubt TINYINT(1) DEFAULT 0";
        echo "- " . $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }

}
?>